package com.wt.admin.service.ai.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wt.admin.code.chat.ModelContent4100;
import com.wt.admin.config.cache.impl.ChatContentCache;
import com.wt.admin.domain.dto.ai.ChatDTO;
import com.wt.admin.domain.dto.ai.ChatModelContentDTO;
import com.wt.admin.domain.entity.ai.ChatModelContentEntity;
import com.wt.admin.domain.vo.ai.ChatModelContentVO;
import com.wt.admin.domain.vo.sys.UserVO;
import com.wt.admin.mapper.ai.ChatModelContentMapper;
import com.wt.admin.service.ai.ChatModelContentService;
import com.wt.admin.util.AssertUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ChatModelContentServiceImpl extends ServiceImpl<ChatModelContentMapper, ChatModelContentEntity> implements ChatModelContentService {

    @Resource
    private ChatModelContentMapper chatModelContentMapper;
    @Resource
    private ChatContentCache<Integer, ChatModelContentVO> chatContents;


    @Override
    public List<ChatModelContentVO> modeContentList(ChatModelContentDTO data, UserVO userVO) {
        return chatModelContentMapper.modeContentList(data, userVO);
    }

    @Override
    public ChatModelContentVO modeContentDel(ChatModelContentDTO data) {
        chatModelContentMapper.deleteById(data.getId());
        chatContents.remove(data.getId());
        return null;
    }

    @Override
    public ChatModelContentEntity findById(Integer contentId) {
        ChatModelContentVO chatModelContentVO = chatContents.get(contentId);
        if(ObjectUtil.isEmpty(chatModelContentVO)){
            ChatModelContentEntity chatModelContentEntity = chatModelContentMapper.selectById(contentId);
            BeanUtil.copyProperties(chatModelContentEntity, chatModelContentVO, "chatName");
        }
        return chatModelContentVO;
    }

    @Override
    public ChatModelContentVO modeContentEdit(ChatDTO data, UserVO user) {
        AssertUtil.Str.isEmpty(data.getMessage(), ModelContent4100.CODE_4100);
        AssertUtil.objIsNull(data.getAgentsId(), ModelContent4100.CODE_4101);
        ChatModelContentEntity.Contents contents = new ChatModelContentEntity.Contents();
        contents.setRole("user");
        contents.setContent(data.getMessage());
        ChatModelContentEntity byId = ObjectUtil.isEmpty(data.getContentId())? null : findById(data.getContentId());
        if (ObjectUtil.isEmpty(byId)) {
            byId = new ChatModelContentEntity();
            String msg = data.getMessage().length() > 20 ? data.getMessage().substring(0,20) : data.getMessage();
            byId.setName(msg);
            byId.setAgentsId(data.getAgentsId());
            byId.setUserId(user.getId());
            byId.setContent(List.of(contents));
        } else {
            byId.getContent().add(contents);
        }
        saveOrUpdate(byId);
        ChatModelContentVO chatModelContentVO = BeanUtil.copyProperties(byId, ChatModelContentVO.class, "chatName");
        chatContents.put(byId.getId(),chatModelContentVO);
        return chatModelContentVO;
    }

    @Override
    public void reply(String msg, ChatDTO data, UserVO user) {
        if (StrUtil.isBlank(msg)) {
            return;
        }
        ChatModelContentEntity byId = findById(data.getContentId());
        if(ObjectUtil.isEmpty(byId)){
            return;
        }
        ChatModelContentEntity.Contents contents = new ChatModelContentEntity.Contents();
        contents.setRole("system");
        contents.setContent(msg);
        byId.getContent().add(contents);
        updateById(byId);
        chatContents.put(byId.getId(), BeanUtil.copyProperties(byId, ChatModelContentVO.class,"chatName"));
    }

    @Override
    public ChatModelContentVO modeContentInfo(ChatModelContentDTO data, UserVO user) {
        ChatModelContentEntity chatModelContentEntity = chatModelContentMapper.selectById(data.getId());
        ChatModelContentVO vo = BeanUtil.copyProperties(chatModelContentEntity, ChatModelContentVO.class,"chatName");
        chatContents.put(data.getId(),vo);
        return vo;
    }


}
